//
//  HMVisitorView.swift
//  sinaweibo
//
//  Created by WGP on 16/4/8.
//  Copyright © 2016年 WGP. All rights reserved.
//

import UIKit

//1.定义代理协议
protocol HMVisitorViewDelegate:NSObjectProtocol
{
//    2.定义代理方法
    func visitorViewDidClickRegisterBtn()
    func visitorViewDidClickLoginBt()
}

class HMVisitorView: UIView {

//    3.定义代理属性
    weak var delegate: HMVisitorViewDelegate?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
//    MARK: - 添加子控件
    private func setupUI()
    {
//        1.添加子控件
        addSubview(iconView)
        addSubview(coverView)
        addSubview(homeView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
//        2.添加约束,先要禁用Autotrsizing,他会自动生成一些约束,可能会和自己手动生成的约束冲突
        iconView.translatesAutoresizingMaskIntoConstraints = false
        coverView.translatesAutoresizingMaskIntoConstraints = false
        homeView.translatesAutoresizingMaskIntoConstraints = false
        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        registerButton.translatesAutoresizingMaskIntoConstraints = false
        loginButton.translatesAutoresizingMaskIntoConstraints = false
        
//        将约束添加到父控件上
//        转轮约束
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1, constant: -40))
//        这该约束
        addConstraint(NSLayoutConstraint(item: coverView, attribute: .Left, relatedBy: .Equal, toItem: self, attribute: .Left, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: coverView, attribute: .Right, relatedBy: .Equal, toItem: self, attribute: .Right, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: coverView, attribute: .Top, relatedBy: .Equal, toItem: self, attribute: .Top, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: coverView, attribute: .Bottom, relatedBy: .Equal, toItem: registerButton, attribute: .Bottom, multiplier: 1, constant: 0))
//        Home约束
        addConstraint(NSLayoutConstraint(item: homeView, attribute: .CenterX, relatedBy: .Equal, toItem: iconView, attribute: .CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: homeView, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1, constant: 0))
//        消息标签约束
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .Top, relatedBy: .Equal, toItem: iconView, attribute: .Bottom, multiplier: 1, constant: 16))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .CenterX, relatedBy: .Equal, toItem: iconView, attribute: .CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 250))
//        注册按钮约束
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Left, relatedBy: .Equal, toItem: messageLabel, attribute: .Left, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Top, relatedBy: .Equal, toItem: messageLabel, attribute: .Bottom, multiplier: 1, constant: 16))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 100))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 35))
        
//        登陆按钮约束
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Top, relatedBy: .Equal, toItem: registerButton, attribute: .Top, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Right, relatedBy: .Equal, toItem: messageLabel, attribute: .Right, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 100))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 35))
    }
    
//    MARK: - 公开方法
    func startRotation()
    {
        let animation:CABasicAnimation = CABasicAnimation(keyPath: "transform.rotation")
        animation.toValue = M_PI*2
        animation.duration = 20
        animation.repeatCount = MAXFLOAT
        
//        让动画完成后不要移除
        animation.removedOnCompletion = false
        
        iconView.layer.addAnimation(animation, forKey: nil)
    }
    
    func setupVisitorViewContent(imageName: String, message: String)
    {
        iconView.image = UIImage(named: imageName)
        
        messageLabel.text = message
        
        homeView.hidden = true
        
        coverView.hidden = true
        
//        sendSubviewToBack(coverView)
//        bringSubviewToFront(coverView)
    }
    
//   MARK: - 按钮点击事件
    @objc private func registerBtnDidClick()
    {
        delegate?.visitorViewDidClickRegisterBtn()
    }
    
    @objc private func loginButtonDidClick()
    {
        delegate?.visitorViewDidClickLoginBt()
    }
    
    
//  MARK: - 懒加载
//    转轮
    private lazy var iconView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
//    遮盖
    private lazy var coverView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
//    小房子
    private lazy var homeView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
//    消息文字
    private lazy var messageLabel:UILabel =
    {
        let label = UILabel()
        
        label.text = "关注一些人!看看有什么惊喜..."
        label.textAlignment = .Center
        label.font = UIFont.systemFontOfSize(18)
        label.numberOfLines = 0
        
        return label
    }()
    
//    注册按钮
    private lazy var registerButton:UIButton  =
    {
        let button = UIButton()
        
        button.setTitle("注册", forState: .Normal)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        
        button.addTarget(self, action: Selector("registerBtnDidClick"), forControlEvents: .TouchUpInside)
        
        return button
    }()
    
//    登陆按钮
    private lazy var loginButton:UIButton =
    {
        let button = UIButton()
        
        button.setTitle("登陆", forState: .Normal)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        button.setTitleColor(UIColor.blackColor(), forState: .Normal)
        
        button.addTarget(self, action: Selector("loginButtonDidClick"), forControlEvents: .TouchUpInside)
        
        return button
    }()
}









